package com.dao;

import com.entity.Employee;
import org.apache.ibatis.annotations.*;

import java.util.Map;

/**
 * @author cj
 * @date 2019/10/14
 */
public interface EmployeeDao {

    // 这种设置相当于在mapper文件中设置了resultType为Employee
    @Select("select id,username from employee where id= #{id}")
    Employee getEmpById(int id);


    //这种设置表示调用Employee(Integer id,String username)的构造函数
    @Select("select id,username from employee where id= #{id}")
    @ConstructorArgs({
            @Arg(id = true, column = "id", javaType = Integer.class),
            @Arg(column = "username", javaType = String.class)
    })
    Employee getEmpById2(int id);


    @Select("select id,username as uname from employee where id= #{id}")
    @Results({
            @Result(column = "uname", property = "username"),
            @Result(column = "id", property = "id", id = true)
    })
    Employee getEmpById3(int id);

    //不设置@Results的id值是可以的,这个方法与上面的getEmpById3都没有设置,
    // @Results的id表示ResultMap的id值,在mapper文件中是必须设置的
    @Select("select id,username as uname from employee where id= #{id}")
    @Results({
            @Result(column = "uname", property = "username"),
            @Result(column = "id", property = "id", id = true)
    })
    Employee getEmpById4(int id);

    // 注解ResultMap用来设定此方法关联到的Mapper文件中ResultMap设置
    @Select("select id,username as uname from employee where id= #{id}")
    @ResultMap("for5")
    Employee getEmpById5(int id);


    //返回类型是Map<Integer,Object>的时候,添加上MapKey会报错.说Integer类型没有id属性的getter方法.
    //可以这样理解: MapKey的主要作用就是把查询的List集合转换为一个Map,此map的key的值是MapKey注解设定的字段值
    // 此值就作为Map集合的key值
    // 在https://mybatis.org/mybatis-3/java-api.html有关于@MapKey注解的简短说明
    @Select("select id,username from employee ")
    @MapKey("id")
    Map<Integer, Object> getEmpById6();

    //map集合中值的情况大致为
    // 1:{id:1,username:"aaa"}
    // 2:{id:2,username:"bbb"}
    @Select("select id,username from employee ")
    @MapKey("id")
    Map<Integer, Map<Integer, Object>> getEmpById7();
}
